package org.swings.tools;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.util.EventListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.swings.base.BasePanel;
import org.swings.builder.ButtonBuilder;
import org.swings.builder.components.Button;

/**
 * <p>
 * <b>Overview:</b>
 * <p>
 * 
 * 
 * <pre>
 * Creation date: Feb 16, 2015
 * @author Amit Kshirsagar
 * @version 1.0
 * @since
 * 
 * <p><b>Modification History:</b><p>
 * 
 * 
 * </pre>
 */

public class ToolsBarSwings extends BasePanel implements FocusListener {
	static Logger log = Logger.getLogger(ToolsBarSwings.class.getName());

	public ToolsBarSwings(String applicationComponentXml,
			EventListener parentListener) {
		this.setName("TOOLBARPANEL");
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.setBorder(BorderFactory.createRaisedBevelBorder());
		if (parentListener == null) {
			parentListener = this;
		}
		loadToolBarButtons(applicationComponentXml, parentListener);
	}

	public void loadToolBarButtons(String applicationComponentXml,
			EventListener parentListener) {
		List<Button> buttonList = ButtonBuilder.getButtonsForPanel(
				"TOOLBARPANEL", applicationComponentXml);
		addButtonsToPanel(this, buttonList, parentListener);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.masterswings.base.BasePanel#actionPerformed(java.awt.event.ActionEvent
	 * )
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		final String command = e.getActionCommand();
		final ActionEvent event = e;
		Runnable actionRunner = new Runnable() {

			@Override
			public void run() {
				if (command.equalsIgnoreCase("OK")) {
				} else if (command.equalsIgnoreCase("SUBMIT")) {
				} else if (command.equalsIgnoreCase("RESET")) {
				} else {
					buttonWithOutAction(event);
				}
			}
		};

		Thread actionThread = new Thread(actionRunner, e.getSource().toString());
		actionThread.start();
	}

	public void buttonWithOutAction(ActionEvent event) {
		JOptionPane
				.showMessageDialog(this.getParent(),
						((JComponent) event.getSource()).getName() + " : "
								+ "Button without action: Modify class "
								+ this.getClass().getName()
								+ " to add action logic...",
						((JComponent) event.getSource()).getName(),
						JOptionPane.INFORMATION_MESSAGE);
	}

	protected HashMap<String, JButton> _buttons = new HashMap<String, JButton>();

	/**
	 * @return the _buttons
	 */
	public HashMap<String, JButton> getButtons() {
		return _buttons;
	}

	public JButton getButton(String key) {
		return _buttons.get(key);
	}

	/**
	 * @param controlPanel
	 * @param buttonList
	 */
	public void addButtonsToPanel(JPanel controlPanel, List<Button> buttonList,
			EventListener parentListener) {
		for (int i = 0; i < buttonList.size(); i++) {
			Button button = buttonList.get(i);
			if (!_buttons.containsKey(button.getButtonAction())) {
				JButton jButton = new JButton(button.getButtonName());
				jButton.setName(button.getButtonName());
				jButton.setActionCommand(button.getButtonAction());
				jButton.addActionListener((ActionListener) parentListener);
				jButton.setToolTipText(button.getButtonToolTip().toString());
				if (button.getButtonDisabled() != null
						&& button.getButtonDisabled().equalsIgnoreCase(
								"Disabled")) {
					jButton.setEnabled(false);
				}

				if (button.getButtonListeners() != null) {
					if (button.getButtonListeners().contains("FocusListener")) {
						jButton.addFocusListener((FocusListener) parentListener);
					}
					if (button.getButtonListeners().contains("MouseListener")) {
						jButton.addMouseListener((MouseListener) parentListener);
					}
				}
				_buttons.put(jButton.getActionCommand(), jButton);
				controlPanel.add(jButton);
			}
		}

	}

	@Override
	public void focusGained(FocusEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void focusLost(FocusEvent e) {
		// TODO Auto-generated method stub

	}
}
